package plus.lgx.textsearcher.searcher.extender;

import plus.lgx.textsearcher.config.SearcherConfig;

/**
 * Class name: LineExtender
 *
 * Create time: 12/17/22 8:28 PM
 *
 * @author lgx
 * @version 1.0
 */
@Deprecated
public class LineExtender extends Extender {

    public LineExtender(SearcherConfig config) {
        super(config);
    }

    private final int maxExtendCharsCount = config.maxExtendCharsCount;

    @Override
    public String extend() {
        int beginInd = Math.max(index.get() - 1, 0);
        int endInd   = Math.min(index.get() + 1, off - 1);
        char b, e;

        for (b = content.charAt(beginInd);
            beginInd > 0 && index.get() - beginInd < maxExtendCharsCount;
            b = content.charAt(--beginInd))
            if (b == '\n') {
                beginInd++;
                break;
            }

        for (e = content.charAt(endInd);
            endInd < off - 1 && endInd - index.get() < maxExtendCharsCount;
            e = content.charAt(++endInd))
            if (e == '\n') {
                endInd--;
                break;
            }

        index.set(Math.min(endInd, off));
        return content.substring(beginInd, Math.min(endInd + 1, off)).trim();
    }

}
